// pages/order/comfirm-order/comfirm-order.js
const controllers=require('../../controllers/controllerOrder').controller
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    image:'',
    name:'',
    price:'',
    imgurl:'',
    array01:[],
    index01:-1,
    array02:['2020-10-29 08:00:00','2020-10-29 18:00:00'],
    index02:-1,
    array03:[],
    index03:-1,
    inputvalue:'',
    limit:0,
    caninput:false,
    userid:[],
    masterid:[],
    choiceuser:0,
    choicemaster:0,
  },
  //选择预约用户
  choiceuser(e){
    console.log(e);
    
    this.setData({
      index01:e.detail.value,
      choiceuser:e.detail.value
    })
  },
  //选择时间
  choicetime(e){
    this.setData({
      index02:e.detail.value
    })
  },
  //选择技师
  choicemaster(e){
    this.setData({
      index03:e.detail.value,
      choicemaster:e.detail.value
    })
  },
  //限制备注字数
  descinput(e){
    console.log(e);
    
    this.setData({
      limit:e.detail.value.length,
      inputvalue:e.detail.value
    })

    if(this.data.limit >=100){
      this.setData({
        caninput:true
      })
    }else{
      this.setData({
        caninput:false
      })
    }
  },
  //提交
  submit(){
    if(this.data.index01 == -1){
      wx.showToast({
        title: '请选择预约用户',
        icon:'none'
      })

      return false
    }
    if(this.data.index02 == -1){
      wx.showToast({
        title: '请选择预约时间',
        icon:'none'
      })

      return false
    }
    if(this.data.index03 == -1){
      wx.showToast({
        title: '请选择预约技师',
        icon:'none'
      })

      return false
    }
    //提交订单
    controllers.comfirmDai({
      goodsId:this.data.goodsId,
      shopId:wx.getStorageSync('shopId'),
      accountRecordId:this.data.userid[this.data.index01],
      shopAccountId:this.data.masterid[this.data.index03],
      desc:this.data.inputvalue,
      agreeTime:this.data.array02[this.data.index02]
    }).then(res =>{
      wx.showToast({
        title: '预约成功',
        icon:'none'
      })
      setTimeout(function(){
        wx.navigateTo({
          url: '/pages/order/service-order/service-order?id=2',
        })
      },1000)
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      image:options.image,
      name:options.name,
      price:options.price,
      goodsId:options.goodsId
    })

    this.setData({
      imgurl:app.globalData.IMG_URL
    })

    //获取用户列表
    controllers.getUser({
      shopId:wx.getStorageSync('shopId')
    }).then(res =>{
      console.log(res);
      var username=[]
      var userid=[]
      res.data.forEach(function(item,index){
        // index01.push(item.accountNick)
        // userid.push(item.id)
        username.push(item.accountNick)
        userid.push(item.id)
      })
      this.setData({
        array01:username,
        userid:userid
      })
      
    })

    //获取技师列表
    controllers.getMaster({
      shopId:wx.getStorageSync('shopId')
    }).then(res =>{
      let arr=[]
      let masterid=[]
      res.data.forEach(function(item,index){
        // index02.push(item.nick)
        // masterid.push(item.id)
        arr.push(item.nick)
        masterid.push(item.id)
      })
      this.setData({
        array03:arr,
        masterid:masterid
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})